//
//  LTLTabBar.m
//  WeiBo
//
//  Created by Apple_Lzzy46 on 16/9/12.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLTabBar.h"

@interface LTLTabBar ()

@property (nonatomic,strong) UIButton *anNiu;

@end

@implementation LTLTabBar
/**
 *  重写初始化方法
 */
-(instancetype)init
{
    if ((self =[super init]) != nil) {
        [self setUI];
    }
    return self;
}
///子控件布局
-(void)layoutSubviews
{

    [super layoutSubviews];
    ///按钮宽度
    CGFloat w             = self.bounds.size.width/5;
    
    __block CGFloat index = 0;

    CGRect rect           = CGRectMake(0, 0, w, self.bounds.size.height);

    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            ////设置frame
            obj.frame             = CGRectOffset(rect, index*w, 0);
            ///三目运算符判断自增量
            index                 += (index == 1) ? 2:1;
        }

    }];
    ///按钮位置
    _anNiu.frame          = CGRectOffset(rect, 2*w, 0);

    [self bringSubviewToFront:_anNiu];
}
/**
 *  按钮懒加载
 *
 *  @return 按钮
 */
-(UIButton *)anNiu
{
    if (_anNiu == nil) {
         _anNiu                = [[UIButton alloc]init];

        [_anNiu setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_anNiu setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_anNiu setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [_anNiu setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [_anNiu sizeToFit];
        
    }

    return _anNiu;
}
/**
 *  添加按钮
 */
-(void)setUI
{
    [self addSubview:self.anNiu];
    [self.anNiu addTarget:self action:@selector(didAnNiu) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)didAnNiu
{
    if ([self.LTLdelegate respondsToSelector:@selector(didAnNiuTabBar:)]) {
        [self.LTLdelegate didAnNiuTabBar:self];
        
    }
    
}
@end
